extends Node2D
tool

signal on_chess_gen(chessName)

signal on_game_win(turnName)

export var rows = 4

export var cols = 4

var gridCls = preload("res://Actors/BoardGrid.tscn")

var blackCls = preload("res://Actors/BlackChess.tscn")

var whiteCls = preload("res://Actors/WhiteChess.tscn")

enum TURN {BLACK, WHITE}
var currentTurn = TURN.BLACK

# name -> chess
var chessMap = {}

# Called when the node enters the scene tree for the first time.
func _ready():
	var startY = 20
	var startX = 20
	
	for rowIdx in rows: # row 0, 1, 2, 3
		startX = 20
		for colIdx in cols: # 0=> 0, 1 => 32			
			var grid = gridCls.instance()
			grid.set("position", Vector2(startX, startY))
			
			if rowIdx == 0 and colIdx == 0:
				grid.grid_type = grid.GRID_TYPE.LEFT_TOP_CORNER
			elif rowIdx == 0 and colIdx == cols - 1:
				grid.grid_type = grid.GRID_TYPE.RIGHT_TOP_CORNER
			elif rowIdx == rows - 1 and colIdx == 0:
				grid.grid_type = grid.GRID_TYPE.LEFT_BOTTOM_CORNER
			elif rowIdx == rows - 1 and colIdx == cols - 1:
				grid.grid_type = grid.GRID_TYPE.RIGHT_BOTTOM_CORNER
			elif rowIdx == 0:
				grid.grid_type = grid.GRID_TYPE.TOP_CROSS
			elif rowIdx == rows - 1:
				grid.grid_type = grid.GRID_TYPE.BOTTOM_CROSS
			elif colIdx == 0:
				grid.grid_type = grid.GRID_TYPE.LEFT_CROSS
			elif colIdx == cols - 1:
				grid.grid_type = grid.GRID_TYPE.RIGHT_CROSS
			
			grid.name = "grid-" + str(rowIdx) + "-" + str(colIdx)
			grid.connect("on_grid_click", self, "OnGridClick")
			
			add_child(grid)
			startX += 32
		startY += 32
		
	connect("on_chess_gen", self, "CheckWin")

func CheckWin(chessName):
	var chess = get_node(chessName)
	# 1 Black, White
	var gps = chess.get_groups()
	if gps.size() > 0:
		var gd = gps[0]
		
		var hasWin = false
		
		var c1 = CheckLineHas5(gd, chessName, Vector2(-1, -1), Vector2(1, 1))
		if c1 >= 5:
			print("Win")
			hasWin = true
		else:		
			var c2 = CheckLineHas5(gd, chessName, Vector2(0, -1), Vector2(0, 1))
			if c2 >= 5:
				print("Win")
				hasWin = true
			else:
				var c3 = CheckLineHas5(gd, chessName, Vector2(1, -1), Vector2(-1, 1))
				if c3 >= 5:
					print("Win")
					hasWin = true
				else:
					var c4 = CheckLineHas5(gd, chessName, Vector2(1, 0), Vector2(-1, 0))
					if c4 >= 5:
						print("Win")
						hasWin = true
		if hasWin:
			emit_signal("on_game_win", gd)
			get_tree().reload_current_scene()
	# 2 Lines check 5

func CheckLineHas5(group:String, chessName:String, decVec:Vector2, incVec:Vector2):
	var result = 1
	# 1 Chess loc
	#   chess-3-4 => 3-4 => 3,4
	var locStr = chessName.lstrip("chess-")
	var locArr = locStr.split("-", false)
	if locArr.size() == 2:
		var currentVec = Vector2(int(locArr[0]), int(locArr[1]))
		for inc in 4:
			currentVec = currentVec + incVec
			var nextName = "chess-" + str(currentVec.x) + "-" + str(currentVec.y)
			if not chessMap.has(nextName):
				break
			var nextChess = chessMap[nextName]
			if not nextChess.is_in_group(group):
				break
			result += 1
			
		currentVec = Vector2(int(locArr[0]), int(locArr[1]))
		for dec in 4:
			currentVec = currentVec + decVec
			var nextName = "chess-" + str(currentVec.x) + "-" + str(currentVec.y)
			if not chessMap.has(nextName):
				break
			var nextChess = chessMap[nextName]
			if not nextChess.is_in_group(group):
				break
			result += 1
	return result

func OnGridClick(gridName:String):
	# grid-3-3  => chess-3-3
	var chessName = "chess-" + gridName.lstrip("grid-")
	if not chessMap.has(chessName):
		var nd = get_node(gridName)
		var chess = null
		if currentTurn == TURN.BLACK:
			chess = blackCls.instance()
			chess.add_to_group("Black")
			currentTurn = TURN.WHITE
		elif  currentTurn == TURN.WHITE:
			chess = whiteCls.instance()
			chess.add_to_group("White")
			currentTurn = TURN.BLACK
		chess.name = chessName
		chess.set("position", nd.position)
		add_child(chess)
		chessMap[chessName] = chess
		emit_signal("on_chess_gen", chessName)
	
	
	
	
	
	
	
	
	
